using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;


namespace BeatEmUp
{

    public class MenuComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        ResolutionManager resManager;
        KeyboardState oldState;
        MouseState oldMouseState;
        
        bool isMenuVisible = true;
        int menuXpos;
        int menuYpos;
        int textWidth =200;
        int textHeight = 40;

        SpriteFont spriteFont;
        Map map;
        Texture2D background;
        Rectangle bgRect;
        Color menuColor = Color.Yellow;

        Texture2D newGameButtonTexture;
        Rectangle newGameRect;

        Texture2D optionsButtonTexture;
        Rectangle optionsRect;

        Texture2D scoresButtonTexture;
        Rectangle scoresRect;

        Texture2D exitButtonTexture;
        Rectangle exitRect;

        public MenuComponent(Game game)
            : base(game)
        { }

        public override void Initialize()
        {
            bgRect = new Rectangle(0, 0, ((Game)this.Game).graphics.PreferredBackBufferWidth, ((Game)this.Game).graphics.PreferredBackBufferHeight);
            
            oldState = Keyboard.GetState();
            resManager = new ResolutionManager((Game)this.Game);

            menuXpos = ((Game)this.Game).graphics.PreferredBackBufferWidth / 2 - 60;
            menuYpos = ((Game)this.Game).graphics.PreferredBackBufferHeight / 2;

            newGameRect = new Rectangle(menuXpos, menuYpos - 100, textWidth, textHeight);

            optionsRect = new Rectangle(
                ((Game)this.Game).graphics.PreferredBackBufferWidth - 100,
                ((Game)this.Game).graphics.PreferredBackBufferHeight - 100,
                100, 
                100);

            scoresRect = new Rectangle(menuXpos, menuYpos-50, textWidth, textHeight);     
            exitRect = new Rectangle(menuXpos, menuYpos , textWidth, textHeight);

            ((Game)this.Game).changeMapEnableParameterTo(false);
            ((Game)this.Game).changeMapVisibleParameterTo(false);
            base.Initialize();
        }


        protected override void LoadContent()
        {
            background = ((Game)this.Game).Content.Load<Texture2D>("menuBackground");
            spriteFont = ((Game)this.Game).Content.Load<SpriteFont>("font");
            newGameButtonTexture = ((Game)this.Game).Content.Load<Texture2D>("newGameButton");
            optionsButtonTexture = ((Game)this.Game).Content.Load<Texture2D>("optionsButton");
            scoresButtonTexture = ((Game)this.Game).Content.Load<Texture2D>("scoresButton");
            exitButtonTexture = ((Game)this.Game).Content.Load<Texture2D>("exitButton");
        }

        public override void Update(GameTime gameTime)
        {
            UpdateInput();
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = ((Game)this.Game).SpriteBatch;
            GraphicsDeviceManager gdm = ((Game)this.Game).graphics;
                 
            if (isMenuVisible == true)
            {
                sb.Begin();

                sb.Draw(background, bgRect, Color.White);
                //resManager.Draw(background, bgRect);
                resManager.Draw(newGameButtonTexture, newGameRect);
                resManager.Draw(optionsButtonTexture, optionsRect);
                resManager.Draw(scoresButtonTexture, scoresRect);
                resManager.Draw(exitButtonTexture, exitRect);

                sb.End();
            }
        }

        private void UpdateInput()
        {
            KeyboardState newState = Keyboard.GetState();
            MouseState newMouseState = Mouse.GetState();


            if (isMenuVisible == true) ((Game)this.Game).IsMouseVisible = true;
            if (isMenuVisible == false) ((Game)this.Game).IsMouseVisible = false;

            if ((newState.IsKeyDown(Keys.Escape))&&(!oldState.IsKeyDown(Keys.Escape)))
            {
                isMenuVisible = !isMenuVisible;
                
                if (isMenuVisible == true) ((Game)this.Game).changeMapVisibleParameterTo(false);
                else ((Game)this.Game).changeMapVisibleParameterTo(true);
            }

            if ((isMenuVisible)&&
                (newMouseState.LeftButton == ButtonState.Pressed)&&
                (!(oldMouseState.LeftButton == ButtonState.Pressed)))
            {
         
                if (newGameRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    isMenuVisible = false;
                    ((Game)this.Game).changeMapVisibleParameterTo(true);
                    ((Game)this.Game).changeMapEnableParameterTo(true);

                }
                if (optionsRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    if (((Game)this.Game).graphics.IsFullScreen) resManager.SwitchToWindowMode();
                    if (!(((Game)this.Game).graphics.IsFullScreen)) resManager.SwitchToFullScreen();

                }
                if (scoresRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    isMenuVisible = false;
                }
                if (exitRect.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    ((Game)this.Game).Exit();
                }
            }
            oldState = newState;
        }

    }
}
